/**
 * Class: Spheric
 *
 * Purpose: 
 *
 * Created: Jan 27, 2010 @ 1:53:32 PM
 * @author Ken Scott
 */
package org.openfantasia.ps.modifier.surfaces;

import org.openfantasia.ps.modifier.Surface;
import org.openfantasia.ps.particle.Particle;
import org.openfantasia.ui.PColor;
import org.openfantasia.ui.PFrame;
import org.openfantasia.util.math.DPoint3;
import org.openfantasia.util.math.RotationMatrix;

public class Cube extends Surface {

	protected DPoint3 position = new DPoint3();

	protected double size = 1;

	protected DPoint3 orientation = new DPoint3();


	public Cube() {
	}


	public Cube(PColor lineColor, PColor fillColor, double friction, double bounce, DPoint3 position, DPoint3 orientation, double size) {
		super(lineColor, fillColor, friction, bounce);
		this.position = position;
		this.orientation = orientation;
		this.size = size;
	}


	@Override
	public void act(Particle particle) {
		DPoint3 pPos = particle.getPosition().copy();
		DPoint3 pDiff = pPos.copy().sub(position);
		double distance = Math.abs(pDiff.mag());
		if (distance <= size) {
			DPoint3 norm = pDiff.normalize();
			DPoint3 velocity = particle.getVelocity().copy();
			double velMag = -velocity.mag() * particle.getBounce() * bounce;
			DPoint3 velDirection = velocity.normalize();
			velDirection = RotationMatrix.rotate(pPos, norm, Math.PI, velDirection);
			particle.setVelocity(velDirection.normalize().scale(velMag));
		}
	}


	@Override
	public void draw(PFrame frame) {
		DPoint3 pPos = position.copy();

		frame.pushStyle();
		frame.pushMatrix();

		frame.translate(pPos);
		frame.rotateX(orientation.p[0]);
		frame.rotateY(orientation.p[1]);
		frame.rotateZ(orientation.p[2]);

		frame.noFill();
		frame.noStroke();
		if (fillColor != null) {
			frame.fill(fillColor);
		}
		if (lineColor != null) {
			frame.stroke(lineColor);
		}

		frame.box(size);

		frame.popMatrix();
		frame.popStyle();
	}

}